package com.gaoyi.crm.adapter.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;

import com.gaoyi.crm.BR;
import com.gaoyi.crm.db.entity.BaseListEntity;
import com.gaoyi.crm.db.entity.PmnWithList;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<B extends ViewDataBinding, D extends BaseListEntity> extends ListAdapter<D, BaseViewHolder> {

    protected final Context mContext;

    public BaseAdapter(Context context) {
        super(new DiffUtil.ItemCallback<D>() {
            @Override
            public boolean areItemsTheSame(@NonNull D oldItem, @NonNull D newItem) {
                if (oldItem instanceof PmnWithList) {
                    return ((PmnWithList) oldItem).pmn.getId().equals(((PmnWithList) newItem).pmn.getId());
                }
                return oldItem.equals(newItem);
            }

            @Override
            public boolean areContentsTheSame(@NonNull D oldItem, @NonNull D newItem) {
                return newItem.isChange();
            }
        });
        this.mContext = context;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        B binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutResId(), parent, false);
        final BaseViewHolder holder = new BaseViewHolder(mContext, binding.getRoot());
        onCreateView(binding, holder);
        binding.getRoot().setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClickListener(v, holder.getLayoutPosition());
            }
        });
        binding.getRoot().setOnLongClickListener(v -> {
            if (onItemLongClickListener != null) {
                onItemLongClickListener.onItemLongClickListener(v, holder.getLayoutPosition());
            }
            return true;
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        B binding = DataBindingUtil.getBinding(holder.itemView);
        if (binding != null) {
            getItem(position).setChange(false);
            binding.setVariable(BR.data, getItem(position));
            setBindingVariable(binding);
            binding.executePendingBindings();
            onBindItem(binding, getItem(position), position);
        }
    }

    protected abstract void setBindingVariable(B binding);

    protected abstract void onBindItem(B binding, D item, int position);

    protected abstract void onCreateView(B binding, BaseViewHolder holder);

    protected abstract @LayoutRes
    int getLayoutResId();

    public void add(D data) {
        List<D> newList = new ArrayList<>(getCurrentList());
        newList.add(data);
        submitList(newList);
    }

    public void delete(int position) {
        List<D> newList = new ArrayList<>(getCurrentList());
        newList.remove(position);
        submitList(newList);
    }

    public void clear() {
        submitList(new ArrayList<>());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    protected OnItemClickListener onItemClickListener;//单击事件

    public interface OnItemClickListener {
        void onItemClickListener(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected OnItemChildrenClickListener onItemChildrenClickListener;

    public interface OnItemChildrenClickListener {
        void onItemChildrenClickListener(View v, int position);
    }

    public void setOnItemChildrenClickListener(OnItemChildrenClickListener onItemChildrenClickListener) {
        this.onItemChildrenClickListener = onItemChildrenClickListener;
    }

    protected OnItemLongClickListener onItemLongClickListener;

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View v, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}
